<?php
	session_start();
	$success=false;

	//inserts new student data into DB
	function submitForm(){
		if ($_SERVER['REQUEST_METHOD'] == 'POST'){
			// Connect to server and select databse.
			mysql_connect("localhost", "root", "")or die("cannot connect"); 
			mysql_select_db("Project 5.5")or die("cannot select DB");

			// details sent from form 
			$mypassword=trim($_POST['password']);
			$myfirstname=trim($_POST['firstname']); 
			$mylastname=trim($_POST['lastname']); 
			$myemail=trim($_POST['email']);
			if (strlen($mylastname)>0) {
				$firstletter=$mylastname[0];
			}else {
				$firstletter="";
			}
			
			$sql="SELECT * FROM users WHERE First_Name='".$myfirstname."' and Last_Name LIKE '".$firstletter."%'";
			$count=''.mysql_num_rows(mysql_query($sql));
			$num=str_pad($count, 2, "0", STR_PAD_LEFT);
			$myusername="".$myfirstname.$firstletter."_".$num;

			$sql="INSERT INTO users (First_Name, Last_Name, Username, Password, Email, Role) VALUES ('$myfirstname', '$mylastname', '$myusername', '$mypassword', '$myemail', 'STU')";
			$result=mysql_query($sql);
			$_SESSION["username"]=$myusername;
			$_SESSION["firstname"]=$myfirstname;
			$_SESSION["lastname"]=$mylastname;
			$_SESSION["email"]=$myemail;
			$_SESSION["role"]='STU';
						
			$sql="SELECT User_ID FROM users WHERE Username='".$myusername."'";
			$result=mysql_fetch_row(mysql_query($sql));
			$sql="INSERT INTO studentinfo (User_ID) VALUES (".$result[0].")";
			$result=mysql_query($sql);
			
			global $success;
			$success=true;
		}
	}
	
	//display message on success
	function onSuccess(){
	global $success;
		if ($success){
			echo "<a href='index.php'>";
			echo '<div id="error" style="display:block;background:#9F9;color:#777;">';
			echo "Enrollment successful, your username is ".$_SESSION["username"].". Click here to continue.";
			echo '</div></a>';
		}
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Student Enrolment</title>	
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<link href="css/main.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="js/formsValidate.js"></script>
</head>
<body>
	<div id="topbar">
		<div id="qut_logo" />  
		<img src="../img/strapline.png" alt="Slogan" id="slogan"/>
	</div>
	<div id="menubar">
		<div id="breadcrumbs">
			<a href="index.php">Home</a> \ <a href="enrol.php">Enrol</a>
		</div>
	</div>
	<?php 
		submitForm();
		onSuccess();
	?>
	<a href="enrol.php">
		<div id="error">
		</div>
	</a>
	<div id="namebar_enrolment"></div>
	<div id="body" style="padding-top:30px">
		
		<form style=" background-color:#FFF; width:500px; margin-left:30%;" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" name="enrolform" id="enrolform" onsubmit="return validateForm('enrolform');">
			<center>
			  <div style="font-Size:16px">Fill out your Details below to enrol in the system.</div><br/>
					<table width="377">
						<tr>
						  <td width="100px">First Name: </td>
						  <td width="144"><input class="form-login" type="text" name="firstname"/></td>
						</tr>
						<tr>
						  <td>Last Name: </td>
						  <td><input class="form-login" type="text" name="lastname"/></td>
						</tr>
						<tr>
						  <td>Email: </td>
						  <td><input class="form-login" type="text" name="email"/></td>
						</tr>
						<tr>
						  <td>Password: </td>
						  <td><input class="form-login" type="password" name="password" /></td>
						</tr>
					</table>
				
				<br/>			
				<input class="subBtn" type="submit" value=""/>
				<a href="login.php">
					<input class="CancelBtn" type="button" name="cancel" value="" />
				</a>
				<br/>
			</center>
		</form>
		<br/><br/>
	</div>
	
	<div id="bottombg">
		<p>
			<a style="color:#333" href="http://www.qut.edu.au/additional/privacy">Privacy</a> 
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/copyright">Copyright</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/disclaimer">Disclaimer</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/accessibility">Accessibility</a>
		</p>
	</div>
</body>
</html>